#include "xmessage.h"
#include <stdio.h>
#include <string.h>
#include "xheader.h"
#include "xcontent.h"
#include "msg.h"

XSIPMessage::XSIPMessage(int type)
	: m_messageType(type)
{
	m_transaction = NULL;
	m_content = NULL;
	m_header = NULL;
}

XSIPMessage::~XSIPMessage()
{
	if (m_header)
		delete m_header;
	m_header = NULL;
	if (m_content)
		delete m_content;
	m_content = NULL;
}

void* XSIPMessage::getObject(const char* name) const
{
	if (strcmp(name, "XSIPMessage") == 0)
		return const_cast<XSIPMessage*>(this);
	else
		return XSIPObject::getObject(name);
}

int XSIPMessage::getRawData(char* data, int size)
{
	getHeader()->addHeader(XSIP_MSG_HEADER_CONTENT_LENGTH, m_content->getSize());
	bool ret = m_header->serializeToArray(data, size);
	int count = m_header->getHeaderSize();
	data[count ++]  ='\r';
	data[count ++]  ='\n';
	data[count ++]  ='\r';
	data[count ++]  ='\n';
	memcpy(data + count, m_content->getData(), m_content->getSize());
	return count + m_content->getSize();
}

int XSIPMessage::getMessageType()
{
	return m_messageType;
}

XSIPTransaction* XSIPMessage::getTransaction()
{
	return m_transaction;
}

void XSIPMessage::setTransaction(XSIPTransaction* transaction)
{
	m_transaction = transaction;
}

XSIPHeader* XSIPMessage::getHeader()
{
	return m_header;
}

void XSIPMessage::setHeader(XSIPHeader* header)
{
	m_header = header;
}

XSIPContent* XSIPMessage::getContent()
{
	return m_content;
}

void XSIPMessage::setContent(XSIPContent* content)
{
	m_content = content;
}